#include "Tool.h"


time_t TimeTool::getCurrentTime()
{
	return time(0);
}

tm * TimeTool::getTimeStruct(const time_t nowTime)
{
	struct tm* now = localtime(&nowTime);
	now->tm_year += 1900;
	now->tm_mon += 1;
	return now;
}


int TimeTool::getYear(const time_t nowTime)
{
	return this->getTimeUnit(this->getTimeStruct(nowTime), YEAR);
}

int TimeTool::getMonth(const time_t nowTime)
{
	return this->getTimeUnit(this->getTimeStruct(nowTime), MONTH);
}

int TimeTool::getDay(const time_t nowTime)
{
	return this->getTimeUnit(this->getTimeStruct(nowTime), DAY);
}

int TimeTool::getHour(const time_t nowTime)
{
	return this->getTimeUnit(this->getTimeStruct(nowTime), HOUR);
}

int TimeTool::getMinute(const time_t nowTime)
{
	return this->getTimeUnit(this->getTimeStruct(nowTime), MINUTE);
}

int TimeTool::getSecond(const time_t nowTime)
{
	return this->getTimeUnit(this->getTimeStruct(nowTime), SECOND);
}

int TimeTool::getYear()
{
	return this->getYear(this->getCurrentTime());
}

int TimeTool::getMonth()
{
	return this->getMonth(this->getCurrentTime());
}

int TimeTool::getDay()
{
	return this->getDay(this->getCurrentTime());
}

int TimeTool::getHour()
{
	return this->getHour(this->getCurrentTime());
}

int TimeTool::getMinute()
{
	return this->getMinute(this->getCurrentTime());
}

int TimeTool::getSecond()
{
	return this->getSecond(this->getCurrentTime());
}

int TimeTool::getTimeUnit(struct tm* tm, int timeEnum)
{
	int result;
	switch (timeEnum)
	{
	case YEAR:
		result = tm->tm_year;
		break;
	case MONTH:
		result = tm->tm_mon;
		break;
	case DAY:
		result = tm->tm_mday;
		break;
	case HOUR:
		result = tm->tm_hour;
		break;
	case MINUTE:
		result = tm->tm_min;
		break;
	case SECOND:
		result = tm->tm_sec;
		break;
	default:
		result = tm->tm_year;
		break;
	}

	return result;
}

std::string TimeTool::getTimeString()
{
	return this->getTimeString(this->getCurrentTime());
}

std::string  TimeTool::getTimeString(time_t tm)
{
	return this->getTimeString(tm, false);
}

std::string TimeTool::getTimeString(time_t tm, bool checkLong)
{
	char arrays[255];
	std::stringstream strtime;
	if (checkLong)
		strftime(arrays, sizeof(arrays), "%Y-%m-%d %H:%M:%S", std::localtime(&tm));
	else
		strftime(arrays, sizeof(arrays), "%Y-%m-%d", std::localtime(&tm));
	strtime << arrays;
	return strtime.str();
}

